package com.shuwen.gcdj.controller.webSocket;

import com.shuwen.gcdj.bean.Home.request.HomeStatusReq;
import com.shuwen.gcdj.bean.Home.response.HomeAnimationSensorResp;
import com.shuwen.gcdj.bean.Home.response.HomeStatusSensorResp;
import com.shuwen.gcdj.bean.Home.response.HomeStatusSensorWebSockResp;
import com.shuwen.gcdj.bean.Home.response.OverlookingHomeResp;
import com.shuwen.gcdj.bean.conditionMonitoring.request.StrainSensorDateNowQueryReq;
import com.shuwen.gcdj.bean.conditionMonitoring.response.StatusSensorDateNowResp;
import com.shuwen.gcdj.bean.sysDictionary.request.SysDictionaryReq;
import com.shuwen.gcdj.bean.sysDictionary.response.SysDictionaryResp;
import com.shuwen.gcdj.common.api.NoticeWebsocketResult;
import com.shuwen.gcdj.common.constant.Constants;
import com.shuwen.gcdj.common.util.webSocket.SimpMessagingTemplateUtils;
import com.shuwen.gcdj.mapper.mysql.sysDictionary.SysDictionaryMapper;
import com.shuwen.gcdj.service.statistic.ConditionMonitoringService;
import com.shuwen.gcdj.service.statistic.FaultMonitoringService;
import com.shuwen.gcdj.service.statistic.HomeService;
import com.shuwen.gcdj.service.statistic.WorkingConditionsService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;


@Api(tags = "webSocket - 定时")
@Component
@Slf4j
public class WebSocketScheduled {
    @Resource
    private SimpMessagingTemplateUtils simpMessagingTemplateUtils;

    @Resource
    private WorkingConditionsService workingConditionsService;

    @Resource
    private FaultMonitoringService faultMonitoringService;

    @Resource
    private HomeService homeService;

    @Resource
    private SysDictionaryMapper sysDictionaryMapper;

    @Resource
    private ConditionMonitoringService conditionMonitoringService;

    @Scheduled(cron = "0/5 * *  * * ?")
    public void socketCol() {
        simpMessagingTemplateUtils.sendMessageToSubject("/topic/greetings/eqwe",
                NoticeWebsocketResult.data("你的世界", "1"));
    }

    //region  首页俯视图
    /*@Scheduled(cron = "0/2 * *  * * ?")
    public void homeStrain() {
        List<OverlookingHomeResp> result = homeService.getOverlookingHome();
        simpMessagingTemplateUtils.sendMessageToSubject("/houduanToQianduan/Home",
                NoticeWebsocketResult.data(result, "1"));
    }*/
    //endregion



    //region 首页应变
    @Scheduled(cron = "0/2 * *  * * ?")
    public void homeStrain1() {

        //获取设备表
        SysDictionaryReq dic = new SysDictionaryReq();
        dic.setParentCode(Constants.CRANE_EQUIPMENT_OBJECT);
        List<SysDictionaryResp> sysDictionarys = sysDictionaryMapper.getList(dic);
        List<HomeStatusSensorWebSockResp> result = new ArrayList<>();

        for (SysDictionaryResp resp : sysDictionarys) {
            HomeStatusSensorWebSockResp homeStatusSensor = new HomeStatusSensorWebSockResp();
            HomeStatusReq req = new HomeStatusReq();
            req.setEquipmentCode(resp.getOperateObject());
            List<HomeStatusSensorResp> datas = homeService.getStrainAsync(req).getData();
            homeStatusSensor.setEquipmentCode(resp.getOperateObject());
            homeStatusSensor.setHomeStatusSensor(datas);
            result.add(homeStatusSensor);
        }

        simpMessagingTemplateUtils.sendMessageToSubject("/houduanToQianduan/homeStrain",
                NoticeWebsocketResult.data(result, "1"));
    }
    //endregion

    //region 首页振动
    @Scheduled(cron = "0/2 * *  * * ?")
    public void homeVibration1() {
        //获取设备表
        SysDictionaryReq dic = new SysDictionaryReq();
        dic.setParentCode(Constants.CRANE_EQUIPMENT_OBJECT);
        List<SysDictionaryResp> sysDictionarys = sysDictionaryMapper.getList(dic);
        List<HomeStatusSensorWebSockResp> result = new ArrayList<>();

        for (SysDictionaryResp resp : sysDictionarys) {
            HomeStatusSensorWebSockResp homeStatusSensor = new HomeStatusSensorWebSockResp();
            HomeStatusReq req = new HomeStatusReq();
            req.setEquipmentCode(resp.getOperateObject());
            List<HomeStatusSensorResp> datas = homeService.getVibrationRealTimeDataSensorAsync(req).getData();
            homeStatusSensor.setEquipmentCode(resp.getOperateObject());
            homeStatusSensor.setHomeStatusSensorVibration(datas);
            result.add(homeStatusSensor);
        }
        simpMessagingTemplateUtils.sendMessageToSubject("/houduanToQianduan/homeVibration",
                NoticeWebsocketResult.data(result, "1"));
    }
    //endregion

    //region 首页动画
//    @Scheduled(cron = "0/2 * *  * * ?")
//    public void homeAnimation1() {
//        //获取设备表
//        SysDictionaryReq dic = new SysDictionaryReq();
//        dic.setParentCode(Constants.CRANE_EQUIPMENT_OBJECT);
//        List<SysDictionaryResp> sysDictionarys = sysDictionaryMapper.getList(dic);
//        List<HomeStatusSensorWebSockResp> result = new ArrayList<>();
//
//        for (SysDictionaryResp resp : sysDictionarys) {
//            HomeStatusSensorWebSockResp homeStatusSensor = new HomeStatusSensorWebSockResp();
//            HomeStatusReq req = new HomeStatusReq();
//            req.setEquipmentCode(resp.getOperateObject());
//            List<HomeAnimationSensorResp> datas = homeService.getHomeAnimationSensorAsync(req).getData();
//            homeStatusSensor.setEquipmentCode(resp.getOperateObject());
//            homeStatusSensor.setHomeAnimationSensor(datas);
//            homeStatusSensor.setCartLocation(datas.stream().findFirst().orElse(new HomeAnimationSensorResp()).getCartLocation());
//            result.add(homeStatusSensor);
//        }
//        simpMessagingTemplateUtils.sendMessageToSubject("/houduanToQianduan/homeAnimation",
//                NoticeWebsocketResult.data(result, "1"));
//    }
    //endregion
}

